package com.train.dao;

import com.train.pojo.User;

import java.sql.*;

public class DaoUser {
    public User getJdbcMysql(String username) throws ClassNotFoundException {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        User user = null;
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/train";
        try {
            //连接数据库
            conn=DriverManager.getConnection(url,"root","20031011LK");
            //预编译
            String sql="select * from user where username = ?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,username);
            //执行sql语句
            rs=ps.executeQuery();
            while(rs.next()){

                user=new User();
                user.setId(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setSex(rs.getString(4));
                user.setPhone(rs.getString(5));
                System.out.println(user);
            }
            rs.close();
            ps.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
}
